home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
050
/
turbo_t1.arc
/
TL03.TXT
< prev
next >
Wrap
Text File
|
1985-06-07
|
8KB
|
211 lines
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 12
TURBO-LESSON 3: PROGRAM STRUCTURE
OBJECTIVES - In this lesson you will learn about:
1. Program structure
2. Compiler Directive, $U+
3. Write and WriteLn statements
4. Comments - at beginning of program
5. Comments - at end of statement line
6. Comments - kill a section of code temporarily
1. Program structure.
PASCAL programs are written in a certain way to make them easier
to write and easier to read. There are two parts to any PASCAL
program: the DECLARATIONS part and the PROGRAM BODY.
The PROGRAM BODY is where the processing statements occur, and
any data item used there must first be defined in the
DECLARATIONS section. The form of a PASCAL program is
PROGRAM Demo; (Program name - not required)
LABEL (Labels, if used, go here)
CONST (Constants are defined here)
TYPE (Define your own data types here)
VAR (ALL variables must be defined here)
BEGIN (Processing statements occur between
END. BEGIN and END.)
The simplest PASCAL program is:
BEGIN
END.
##### DO:
Type in the program:
BEGIN
END.
Exit the editor and type R to execute the program.
What happened? (If you hold the R key down to run the program
over and over, you may see the message, "running").
Notice that no compile errors or run time errors occurred
(evidenced by the absence of error messages on the screen).
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 13
Definitely not the most useful program, but it illustrates a
point: the MAIN BODY is always required and as much of the
DECLARATIONS section as needed to define the data used. Since
this simple program uses no data, the DECLARATIONS section can
be omitted.
Standard PASCAL requires that declarations occur in the order
specified and each type of declaration may occur only once.
TURBO is a little more forgiving in some respects than standard
PASCAL (declarations don't have to occur in the specified order,
and the same type of declaration may occur more than once).
2. Compiler Directive, $U+
Compiler directives are used to control special features provided
by the compiler. The directive, $U+, which occurs at the
beginning of most of the sample programs, ensures that you will
be able to stop a program without rebooting.
3. Write and WriteLn statements.
These processing statements are introduced first, because they
can be used with messages which require no previous data
declaration. This allows you to experiment with some programming
in the MAIN BODY without any concern for the DECLARATIONS part.
##### DO:
Load PROG3 and use the editor to examine the program. A part of
PROG3 is printed below:
BEGIN
WriteLn('* * * * * * * * * * * * * * * * * *'); {Note that comments }
WriteLn('* *'); {placed at the end of a }
WriteLn('* TURBO-LESSON 3 *'); {statement line should }
WriteLn('* *'); {be terminated on the }
WriteLn('* Edited, Compiled, Executed by *'); {same line. A multiline }
WriteLn('* (put your name here) *'); {comment, like the one at}
WriteLn('* *'); {the beginning of this }
WriteLn('* * * * * * * * * * * * * * * * * *'); {program would include }
{ some of the non-comment statements, making them ineffective. }
In this section of PROG3, the WriteLn statement is used to
display messages.
##### DO:
RUN the program to see the messages displayed on the screen.
(Ctrl-K, Ctrl-D to exit editor, then R to run the program. Refer
to lessons 1 and 2 and your reference manual for more help using
TURBO and the editor.)
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 14
##### DO:
Use the editor to insert your name in the line indicated and
run the program again.
To illustrate the difference between WriteLn and Write
statements, another portion of the program is included below:
WriteLn;
Write ('Check carefully when ');
Write ('you run this program. ');
Write ('How many lines ');
WriteLn('are printed');
WriteLn('by this last set of Write and WriteLn statements?');
The "Ln" in WriteLn may be viewed as a linefeed or "return the
cursor to the beginning of the next line". Note that the
instruction is WriteLn, not LnWrite. The order is significant.
Any message, or other data item within the parentheses following
WriteLn is printed first, the "Write" part, then the cursor is
moved to the next line, the "Ln" part.
Notice the first WriteLn in the program segment above. There is
nothing in parentheses to print, so this statement is a way to
print a blank line.
The next three Write statements write on the same line. Then the
following WriteLn statement writes it's message and causes the
cursor to return to the beginning of the next line.
The last WriteLn displays it's message on the line where the
cursor is positioned, and returns the cursor to the next line.
##### DO:
Add some WriteLn statements after the BEGIN to insert several
blank lines at the top of the screen to push the boxed message
down the screen.
Run the program. Did it work right?
4. Comments - at the beginning of program.
There are two ways to indicate comments in a PASCAL program. The
curly brackets, { }, may be used to enclose comments. The
original comment delimiters, from standard PASCAL are (* *).
At the beginning of a program, or anywhere that multiple line
comments are needed, it is convenient to mark the whole block of
comments with the delimiters at the left margin. This makes it
easy to insert or delete comments within the block.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 15
##### DO:
Add some comments at the beginning of the program. Try both types
of comment delimiters, { }, and (* *).
5. Comments - at the end of a statement line.
Look at the comments following the WriteLn statements in PROG3.
Short comments may be inserted to clarify the action of one or
more statements. NOTICE CAREFULLY that each of these comments
has to be completed on the same line, if the next line is another
active processing statement.
##### DO:
Add a short comment, {Blank Line}, after one of the WriteLn statements which
prints a blank line.
6. Comments - kill a section of code.
Having two sets of comment delimiters makes it possible to use
one set for most comments, while reserving the second set for
"killing" sections of statements when debugging a program.
I prefer to use the { } for most comments, and (* *) for killing
code. You should keep the delimiters used for commenting out
(killing) code in the left margin where they are very visible.
##### DO:
Kill the three WriteLn statements following the one which prints
TURBO-LESSON 3.
Run the program to see the results.
DON'T BE AFRAID TO EXPERIMENT WITH THE SAMPLE PROGRAMS. YOU CAN
ALWAYS GET ANOTHER COPY, FROM YOUR BACKUP DISK (you did make one,
didn't you), FROM THE .BAK FILE PROVIDED BY TURBO, OR FROM THE
ORIGINAL FILE IF YOU HAVEN'T SAVED AN EDITED COPY.
î